package com.spzx.product.controller;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;
@Operation(summary = "分页查询商品SPU列表")
    @GetMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<Product> list =productService.selectProductList();
        return  getDataTable(list);
    }

@Operation(summary = "保存商品(SPU,SKU列表,SPU详情,库存)")
    @PostMapping
    public AjaxResult product(@RequestBody Product product){
    productService.saveProduct(product);
    return success();
}

@Operation(summary = "商品的上架或下架")
    @GetMapping("/updateStatus/{spuId}/{status}")
    public AjaxResult updateStatus(@PathVariable("spuId") Long spuId
        ,@PathVariable("status") Integer status)
{
    if(status !=1 && status !=-1){
        throw new ServiceException("状态值有误");
    }
    productService.updateStatus(spuId,status);
    return success();
}

@Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(description = "商品id列表",example="[1,2]") @PathVariable List<Long> ids)
{
    productService.removeProduct(ids);
    return success();
}

}
